package com.musichord.core.track;

import java.util.List;

import javax.sound.midi.MidiEvent;
import javax.sound.midi.Track;

import com.musichord.core.event.MCEvent;

public class MCTrack {
	protected Track track;

	public MCTrack(Track track) {
		this.track = track;
	}

	/** Add MIDI events with relative ticks to a specific tick in the track */
	public void addEvents2TrackAt(List<MidiEvent> midiEvents, long tick) {
		for (MidiEvent midiEvent : midiEvents) {
			midiEvent.setTick(midiEvent.getTick() + tick);
			track.add(midiEvent);
		}
	}

	/** Add ordered MIDI events to the end of the track */
	public void addEvents2Track(List<MidiEvent> midiEvents) {
		addEvents2TrackAt(midiEvents, track.ticks());
	}

	/** Add one MCEvent to a specific tick in the track */
	public void addMCEvent2TrackAt(MCEvent event, long tick) {
		for (MidiEvent midiEvent : event.getMidiEvents()) {
			midiEvent.setTick(midiEvent.getTick() + tick);
			track.add(midiEvent);
		}
	}

	/** Add one MCEvent to the end of the track */
	public void addMCEvent2Track(MCEvent event) {
		addEvents2TrackAt(event.getMidiEvents(), track.ticks());
	}

	/** Add unordered MCEvents to the end of the track one by one */
	public void addMCEvents2Track(List<MCEvent> events) {
		for (MCEvent event : events) {
			addMCEvent2Track(event);
		}
	}

}
